 package scripting.handler;

 import configs.ServerConfig;
 import database.DatabaseScriptAPI;
 import handling.channel.ChannelServer;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import javax.script.Invocable;
 import javax.script.ScriptEngine;
 import javax.script.ScriptException;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import scripting.AbstractScriptManager;

 public class HandlerScriptManager
   extends AbstractScriptManager {
/*  18 */   private static Logger log = LogManager.getLogger();

   private static HandlerScriptManager instance;
/*  21 */   private final Map<String, Invocable> handlerEvents = new LinkedHashMap<>();

   public static HandlerScriptManager getInstance() {
/*  24 */     if (instance == null) {
/*  25 */       instance = new HandlerScriptManager();
     }
/*  27 */     return instance;
   }

   private String getFullPath(String filename) {
/*  31 */     return "handler/" + filename + ".js";
   }

   public void init() {
/*  35 */     this.handlerEvents.forEach((script, iv) -> {
           try {
             iv.invokeFunction("dispose", new Object[] { null });
/*  38 */           } catch (ScriptException|NoSuchMethodException scriptException) {}
         });

/*  41 */     this.handlerEvents.clear();
/*  42 */     String[] scripts = ServerConfig.CHANNEL_HANDLERS.trim().split(",");
/*  43 */     for (String script : scripts) {
/*  44 */       if (!script.isEmpty()) {
/*  45 */         Invocable iv = getHandlerInvocable(getFullPath(script));
/*  46 */         if (iv != null) {
/*  47 */           this.handlerEvents.put(script, iv);
         }
       }
     }
/*  51 */     this.handlerEvents.forEach((script, iv) -> {
           try {
             ((ScriptEngine)iv).put("hm", this);
             ((ScriptEngine)iv).put("channels", ChannelServer.getAllInstances());
             iv.invokeFunction("init", new Object[] { null });
/*  56 */           } catch (Exception ex) {
             log.error("装载handler脚本出现错误[" + script + "]", ex);
           }
         });
   }

   public Object handler(String name, Object... args) {
/*  63 */     if (this.handlerEvents.containsKey(name)) {
       try {
/*  65 */         return ((Invocable)this.handlerEvents.get(name)).invokeFunction("handler", args);
/*  66 */       } catch (ScriptException|NoSuchMethodException e) {
/*  67 */         log.error("执行handler脚本出现错误[" + name + "]", e);
       }
     }
/*  70 */     return null;
   }








   public List<Map<String, Object>> sql_Select(String sql, Object... data) {
/*  81 */     return DatabaseScriptAPI.SQLSelect(sql, data);
   }








   public int sql_Insert(String sql, Object... data) {
/*  92 */     return DatabaseScriptAPI.SQLInsert(sql, data);
   }








   public int sql_Update(String sql, Object... data) {
/* 103 */     return DatabaseScriptAPI.SQLUpdate(sql, data);
   }
 }


/* Location:              C:\PGX\Server.jar!\scripting\handler\HandlerScriptManager.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */